const api = require("../../../utils/apiUtils");
const app = getApp();

Page({
  data: {
    lists:[1,5,10,20,30,40,50,100,0.1],
    info:{},
    flag:true,
    isBack:true,
    deviceSn:'',
    slot:0,
    modalName:null,
    getOutPrice:0
  },
  _init(){
    const self = this;
    api.post(api.url.walletGet,{
      memberId:app.globalData.userInfo.memberId
    },(res)=>{
      if(res.data.code==200){
        self.setData({
          info:res.data.body
        })
      }
    })
  },
  _pay(money){
    let self = this;
    my.showLoading({
      title: '加载中...',
    })
    api.post(api.url.walletRecharge,{
      memberId:app.globalData.userInfo.memberId,
      money:money,
      appId:my.getAppIdSync().appId,
      payType:2
    },(res)=>{
      console.log(res)
      if(res.data.code==200){
        const tradeNo = res.data.body.tradeNo;
        my.tradePay ({
          tradeNO: tradeNo,
          success: res => {
            console.error('调用 my.tradePay 成功: ', JSON.stringify(res));
            if(res.resultCode == 9000 ||res.resultCode == 8000){
              my.hideLoading()
              self._payResult(tradeNo);
            }else{
              my.hideLoading()
              my.showToast({
                type: 'error',
                content: res.memo || '支付失败',
                mask:true
              })
            }
          },
          fail: error => {
            console.error('调用 my.tradePay 失败: ', JSON.stringify(error));
            my.hideLoading();
            my.showToast({
              type: 'error',
              content: '支付失败',
              mask:true
            })
          },
        });
      }else{
        my.showModal({
          title:'充值失败',
          content:res.data.msg,
          showCancel:false
        })
      }
    })
  },
  _payResult(tradeNo){
    my.showLoading({
      title: '支付中...',
    })
    const _self = this;
    api.post(api.url.walletRechargeResult,{
      tradeNo:tradeNo,
      payType:2
    },res=>{
        if(res.data.code == 200){
          if(res.data.body.resultCode == 2){
              if(_self.data.timer){
                  clearInterval(_self.data.timer);
                  _self.data.timer = null;
              }
              my.showModal({
                title:'温馨提示',
                content: '充值成功～～',
                showCancel:false,
                complete: (res) => {
                    _self._init();
                }
            })
          }
        }else if(res.data.msg){
          my.hideLoading();
          my.showModal({
              content: res.data.msg,
              showCancel:false,
              complete: (res) => {
                  _self._init();
              }
          })
        }else{
            my.hideLoading();
            my.showToast({
              type:'error',
              content: '获取信息失败'
            })
            _self._init();
        }
    })
  },
  hideModal(){
    this.setData({
        modalName:null
    })
  },
  showModal(e){
      this.setData({
          modalName:e.currentTarget.dataset.str
      })
  },
  btnChangeInput(e){
      this.setData({
          getOutPrice:e.detail.value
      })
  },
  btnAllGetOut(){
      this.setData({
          getOutPrice:this.data.info.money
      })
  },
  //申请提现
  btnWalletApply(){
      const getOutPrice = parseFloat(this.data.getOutPrice);
      const self = this;
      if(getOutPrice<=0 || getOutPrice>parseFloat(this.data.info.money||0)){
          self.setData({
              modalName:null
          })
          my.showModal({
              title: '提现失败',
              content: '提现金额必须大于0，并且小于当前余额',
              showCancel:false,
          })
          return;
      }
      api.post(api.url.walletApply,{
          price:getOutPrice
      },res=>{
          console.log(res)
          if(res.data.code==200){
              my.showToast({
                  content: '申请提交成功！'
              })
              self._init();
          }else{
              my.showModal({
                title: '提现失败',
                content: res.data.msg,
                showCancel:false
              })
          }
          self.setData({
              modalName:null,
              getOutPrice:0
          })
      })
  },
  toDevPage(){
      const {deviceSn,slot} = this.data;
      my.redirectTo({
          url:`/packageA/pages/cdcz/select/select?deviceSn=${deviceSn}&num=${slot}`
      })
  },
  btnPay(e){
    if(!this.data.flag) return;
    const num = e.currentTarget.dataset.num;
    let self = this;
    this.setData({
      flag:false
    })
    my.showModal({
      title:'充值提示',
      content:`确定充值 ${num} 元吗`,
      success(res){
        if(res.confirm){
          self._pay(num)
        }
      },
      complete(){
        self.setData({
          flag:true
        })
      }
    })
  },
  toHistoryList(){
    my.navigateTo({
      url: "../operate/operate",
    })
  },
  onLoad(options) {
    if(options && options.deviceSn){
      this.setData({
        isBack:false,
        deviceSn:options.deviceSn,
        slot:options.slot
      })
    }
    this._init();
  },
});
